import java.util.ArrayList;
import java.util.Observable;

public class KitchenToHatch extends Observable implements Runnable
{
	ArrayList<FoodOrder> hatch ;
	HatchToTable htt = new HatchToTable();
	int dishcount = 0;
	boolean start = false;
	String hatchrep = "";
	FoodOrder tableOrder ;
	Thread t ;

	public void run()
	{
		try
		{
			if(!hatch.isEmpty())
			{
				dishcount++;
				if(dishcount %3 ==0)
				{
					tableOrder = new FoodOrder(hatch.remove(0));	// deletes the first element of the Array List in every iteration 	
					htt.start(tableOrder);
				}
				Thread.sleep(1500);
				setChanged();
				notifyObservers();
		    	clearChanged();
			}
			else
				Thread.sleep(3000);
		}
		catch (NumberFormatException nfe) {
			String error = "Number conversion error in  - " + nfe.getMessage();
			System.out.println(error);
		}
		// this catches missing items if only one or two items
		// other omissions will result in other errors
		catch (ArrayIndexOutOfBoundsException e) {
			String error = "Error  : '" + "' index position : "
					+ e.getMessage();
			System.out.println(error);
		} 
		catch (InterruptedException e) 
		{
			e.printStackTrace();
		}
		
	}	

	public void hatchUpdate(ArrayList<FoodOrder> f)
	{	
		hatch = f;	
	}
	
	public String displayHatch()
	{
		if(hatch.isEmpty())
		{
			System.out.println(hatch.get(0).getItem());
			hatchrep += "Nothing in hatch\n"; 
		}
		else
		{
			for(FoodOrder f : hatch)		
			{
				System.out.println(String.format("%-10s",f.getTableName()) + "\t" + String.format("%-30s",f.getItem()) 
				+ "\t" + String.format("%-10s",f.getQuantity()) + "\n");
				
			}
		}
		return hatchrep;
		
	}
}
